package com.example.hzq.zqb.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.hzq.zqb.R;
import com.example.hzq.zqb.adapter.FqzsAdapter;
import com.example.hzq.zqb.bean.GoodsBean;
import com.example.hzq.zqb.model.Model;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.zqb.activity.LoginActivity.u_id;

/**
 * Created by hzq on 2017/7/14.
 */

public class AddtaskActivity extends AppCompatActivity {
    private ImageButton back;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager manager;
    private FqzsAdapter fqzsAdapter;
    private List<GoodsBean> list=null;
    private int page,lastVisibleItemPosition;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addtask);
        init();
    }
    private void init(){
        page=1;
        back=(ImageButton)findViewById(R.id.back1);
        recyclerView=(RecyclerView)findViewById(R.id.task_recycler);
        manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        fqzsAdapter=new FqzsAdapter(AddtaskActivity.this,"add");
        fqzsAdapter.setList(list);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Model model=new Model();
        Call<List<GoodsBean>> call=model.getBuyGoodsList(page,u_id);
        call.enqueue(new Callback<List<GoodsBean>>() {
            @Override
            public void onResponse(Call<List<GoodsBean>> call, Response<List<GoodsBean>> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    list=response.body();
                    fqzsAdapter.setList(list);
                }
            }

            @Override
            public void onFailure(Call<List<GoodsBean>> call, Throwable t) {
                Toast.makeText(AddtaskActivity.this,"404.addtask", Toast.LENGTH_SHORT).show();
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()){
                    page+=1;
                    Model model=new Model();
                    Call<List<GoodsBean>>call=model.getBuyGoodsList(page,u_id);
                    call.enqueue(new Callback<List<GoodsBean>>() {
                        @Override
                        public void onResponse(Call<List<GoodsBean>> call, Response<List<GoodsBean>> response) {
                            if (response.isSuccessful()&&response.body()!=null){
                                list.addAll(response.body());
                                fqzsAdapter.setList(list);
                            }
                        }

                        @Override
                        public void onFailure(Call<List<GoodsBean>> call, Throwable t) {
                        }
                    });
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=list.size()-1;
            }
        });
        recyclerView.setAdapter(fqzsAdapter);
    }
}
